package com.tian.view.anim.interpolator

import android.os.Bundle
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnticipateInterpolator
import android.view.animation.AnticipateOvershootInterpolator
import android.view.animation.BounceInterpolator
import android.view.animation.CycleInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.view.animation.OvershootInterpolator
import android.view.animation.TranslateAnimation
import androidx.appcompat.app.AppCompatActivity
import com.tian.view.databinding.ActivityInterpolatorBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 15:10
 *
 * Interpolator 插值器 改变动画的快慢节奏
 */
class InterpolatorActivity : AppCompatActivity() {

    private lateinit var b: ActivityInterpolatorBinding
    private val anim: Animation by lazy {
        val anim = TranslateAnimation(Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_PARENT, 0.6f,
                Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 0f)
        anim.duration = 2000L
        anim
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.title = "插值器"
        b = ActivityInterpolatorBinding.inflate(layoutInflater)
        setContentView(b.root)
        initView()
    }

    private fun initView() {
        b.mbtAccelerateDecelerate.setOnClickListener {
            anim.interpolator = AccelerateDecelerateInterpolator()
            startAnimator()
        }
        b.mbtAccelerate.setOnClickListener {
            anim.interpolator = AccelerateInterpolator()
            startAnimator()
        }
        b.mbtDecelerate.setOnClickListener {
            anim.interpolator = DecelerateInterpolator()
            startAnimator()
        }
        b.mbtLinear.setOnClickListener {
            anim.interpolator = LinearInterpolator()
            startAnimator()
        }
        b.mbtBounce.setOnClickListener {
            anim.interpolator = BounceInterpolator()
            startAnimator()
        }
        b.mbtAnticipate.setOnClickListener {
            anim.interpolator = AnticipateInterpolator()
            startAnimator()
        }
        b.mbtOvershoot.setOnClickListener {
            anim.interpolator = OvershootInterpolator()
            startAnimator()
        }
        b.mbtAnticipateOvershoot.setOnClickListener {
            anim.interpolator = AnticipateOvershootInterpolator()
            startAnimator()
        }
        b.mbtCycle.setOnClickListener {
            anim.interpolator = CycleInterpolator(3f)
            startAnimator()
        }
        b.mbtCustom.setOnClickListener {
            anim.interpolator = CustomInterpolator()
            startAnimator()
        }
    }

    private fun startAnimator() {
        if (anim.hasStarted()) {
            anim.cancel()
        }
        b.btAnim.startAnimation(anim)
    }


    abstract inner class AnimationListenerAdapter : AnimationListener {
        override fun onAnimationStart(animation: Animation?) {}
        override fun onAnimationEnd(animation: Animation?) {}
        override fun onAnimationRepeat(animation: Animation?) {}
    }
}